package mil.cdc.windows;

import java.awt.Color;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import mil.cdc.listeners.CustomMouseListener;
import mil.cdc.main.Main;
import mil.cdc.utils.CDCConstants;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

//VS4E -- DO NOT REMOVE THIS LINE!
public class EndWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea textArea;
	private JScrollPane textPane;
	private JButton reviewBtn;
	private JButton endBtn;
	private JButton againBtn;
	
	private MainWindow mainWindow;
	private JButton displayBtn;
	private JButton printBtn;
	
	private boolean passed = true;

	public EndWindow() {
		initComponents();
	}
	
	public void attachToMainWindow(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}

	private void initComponents() {
		setTitle("Finished");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/mil/cdc/data/ico/af.gif")));
		setResizable(false);
		setLayout(new GroupLayout());
		add(getTextPane(), new Constraints(new Bilateral(13, 12, 22), new Leading(11, 118, 10, 10)));
		add(getReviewBtn(), new Constraints(new Trailing(12, 205, 205), new Leading(135, 12, 12)));
		add(getEndBtn(), new Constraints(new Leading(13, 180, 12, 12), new Leading(169, 10, 10)));
		add(getAgainBtn(), new Constraints(new Trailing(12, 180, 80, 205), new Leading(167, 12, 12)));
		add(getDisplayBtn(), new Constraints(new Leading(13, 204, 204), new Leading(135, 12, 12)));
		add(getPrintBtn(), new Constraints(new Bilateral(159, 157, 81), new Leading(201, 12, 12)));
		setSize(397, 259);
	}

	private JButton getPrintBtn() {
		if (printBtn == null) {
			printBtn = new JButton();
			printBtn.setText("Print");
			printBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.OPEN_PRINT));
		}
		return printBtn;
	}

	private JButton getDisplayBtn() {
		if (displayBtn == null) {
			displayBtn = new JButton();
			displayBtn.setText("Display Missed Questions");
			displayBtn.setToolTipText("Print out the missed questions");
			displayBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.DISPLAY_QUESTIONS));
		}
		return displayBtn;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setLineWrap(true);
			textArea.setWrapStyleWord(true);
			textArea.setEnabled(false);
			textArea.setFocusable(false);
			textArea.setDisabledTextColor(new Color(51, 51, 51));
		}
		return textArea;
	}

	private JButton getAgainBtn() {
		if (againBtn == null) {
			againBtn = new JButton();
			againBtn.setText("Again");
			againBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.AGAIN));
		}
		return againBtn;
	}

	private JButton getEndBtn() {
		if (endBtn == null) {
			endBtn = new JButton();
			endBtn.setText("End");
			endBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.CLOSE));
		}
		return endBtn;
	}

	private JButton getReviewBtn() {
		if (reviewBtn == null) {
			reviewBtn = new JButton();
			reviewBtn.setText("Review Missed Questions");
			reviewBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.REVIEW_QUESTIONS));
		}
		return reviewBtn;
	}

	private JScrollPane getTextPane() {
		if (textPane == null) {
			textPane = new JScrollPane();
			textPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			textPane.setViewportView(getTextArea());
		}
		return textPane;
	}
	
	public void setMessage(String message) {
		textArea.setText(message);
	}
	
	public void reloadTest() {
		mainWindow.start();
		this.dispose();
	}
	
	public void viewMissedQuestions() {
		mainWindow.viewMissedQuestions();
		this.dispose();
	}
	
	public void displayMissedQuestions() {
		ReviewWindow review = new ReviewWindow();
		review.loadReviewQuestion(mainWindow.getReviewQuestions());
		review.setVisible(true);
	}
	
	public void close() {
		mainWindow.dispose();
		Main.close();
		this.dispose();
	}
	
	
	public void setPass(boolean pass) {
		this.passed = pass;
	}

	public void openPrintOption() {
		
		String area = textArea.getText();
		
		if (passed) {
			area = "Congratulations you passed. Please print the results.\n" + area; 
		}
		else {
			area = "Sorry you did not meet the miminum passing score.\nPlease Try again.\n" + area; 
		}
		
		PrintWindow pw = new PrintWindow(area);
		pw.setVisible(true);
		
	}
}
